cap log close 
log using "${logpath}stacked_regression.log", replace

/*******************************************************************************
stacked_regression.do

Analyzes district-level data using stacked regression
*******************************************************************************/

clear all

qui {
	use "${cleandatapath}dist_data.dta", clear
	drop if dist_code==14 // Dropping Connecticut

	foreach type in crim civ PA {
		levelsof `type'_date, local(`type'_waves)
		local i=1
		foreach wave of local `type'_waves {
			preserve
			keep if `type'_date==`wave' | missing(`type'_date)
			tab district if !missing(`type'_date)
			gen wave=mofd(`wave')
			gen treated=!missing(`type'_date)
			gen months_since_`type'=month_date-wave
			keep if months_since_`type'>=-24 & months_since_`type'<=23
			su months_since_`type'
			if r(min)==-24 & r(max)==23 {
			tab district if !missing(`type'_date)
				keep wave dist_code treated months_since_`type' prior_auth post_civ post_crim ambulance_id active_firms tot_pmt month_date log_rides log_active_firms log_tot_pmt
				tempfile `type'wave`i'
				save ``type'wave`i''
				local i = `i'+1
			}
			restore
		}
	}

	*Civil Litigation

	use `civwave1', clear
	append using `civwave2'
	append using `civwave3'
	append using `civwave4'
	append using `civwave5'
	append using `civwave6'
	append using `civwave7'
	append using `civwave8'
	append using `civwave9'
	append using `civwave10'

	gen months_since100=months_since_civ+100
	replace months_since100=50 if treated==0
	egen dist_wave=group(dist_code wave)
	egen month_wave=group(month_date wave)
	gen post_together=months_since100
	replace post_together=100 if months_since100>100 & !missing(months_since100)
}

qui reghdfe log_tot_pmt ib99.months_since100 i.prior_auth i.post_crim, a(dist_wave month_wave) cluster(dist_code)
coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopts(recast(rcap) color(navy)) ///
	xti(Months Since First Civil Enforcement) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Total Payments in District (Logs)") ///
	ylab(, angle(0) nogrid) ///
	graphregion(color(white)) bgcolor(white)
qui {
	graph export ${outpath}Figure_A5c.pdf, as(pdf) replace // Figure A5c
	reghdfe log_tot_pmt ib99.post_together i.prior_auth i.post_crim, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_civil_log_pmt, replace

	reghdfe log_rides ib99.post_together i.prior_auth i.post_crim, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_civil_log_rides, replace

	*Criminal Litigation

	use `crimwave1', clear
	append using `crimwave2'
	append using `crimwave3'
	append using `crimwave4'
	append using `crimwave5'
	append using `crimwave6'
	append using `crimwave7'
	append using `crimwave8'
	append using `crimwave9'
	append using `crimwave10'
	append using `crimwave11'
	append using `crimwave12'

	gen months_since100=months_since_crim+100
	replace months_since100=99 if treated==0
	egen dist_wave=group(dist_code wave)
	egen month_wave=group(month_date wave)
	gen post_together=months_since100
	replace post_together=100 if months_since100>100
}

qui reghdfe log_tot_pmt ib99.months_since100 i.prior_auth i.post_civ, a(dist_wave month_wave) cluster(dist_code)
coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopts(recast(rcap) color(navy)) ///
	xti(Months Since First Criminal Enforcement) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Total Payments in District (Logs)") ///
	ylab(, angle(0) nogrid) ///
	graphregion(color(white)) bgcolor(white)
qui {
	graph export ${outpath}Figure_A5d.pdf, as(pdf) replace // Figure A5d
	reghdfe log_tot_pmt ib99.post_together i.prior_auth i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_criminal_log_pmt, replace

	reghdfe log_rides ib99.post_together i.prior_auth i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_criminal_log_rides, replace

	*Prior Authorization

	use `PAwave1', clear
	append using `PAwave2'

	gen months_since100=months_since_PA+100
	replace months_since100=99 if treated==0
	egen dist_wave=group(dist_code wave)
	egen month_wave=group(month_date wave)
	gen post_together=months_since100
	replace post_together=100 if months_since100>100

	reghdfe tot_pmt ib99.post_together i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_tot_pmt, replace
}

qui reghdfe log_tot_pmt ib99.months_since100 i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopts(recast(rcap) color(navy)) ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Total Payments in District (Logs)") ///
	ylab(, angle(0) nogrid) ///
	graphregion(color(white)) bgcolor(white)
qui {
	graph export ${outpath}Figure_A5a.pdf, as(pdf) replace // Figure A5a
	reghdfe log_tot_pmt ib99.post_together i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_log_pmt, replace

	reghdfe ambulance_id ib99.post_together i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_tot_rides, replace
	reghdfe log_rides ib99.post_together i.prior_auth i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_log_rides, replace

	reghdfe active_firms ib99.post_together i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_tot_firms, replace
}

qui reghdfe log_active_firms ib99.months_since100 i.post_crim i.post_civ, a(dist_wave month_wave) cluster(dist_code)
coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopts(recast(rcap) color(navy)) ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Active Firms in District (Logs)") ///
	ylab(, angle(0) nogrid) ///
	graphregion(color(white)) bgcolor(white)
qui {
	graph export ${outpath}Figure_A5b.pdf, as(pdf) replace // Figure A5b
	reghdfe log_active_firms ib99.post_together i.prior_auth i.post_civ, a(dist_wave month_wave) cluster(dist_code)
	estadd ysumm
	estimates save ${estpath}stacked_PA_log_firms, replace

	*Table A4
	estimates clear
	estimates use ${estpath}stacked_PA_log_pmt
	eststo est1
	estimates use ${estpath}stacked_PA_tot_pmt
	eststo est2
	estimates use ${estpath}stacked_PA_log_rides
	eststo est3
	estimates use ${estpath}stacked_PA_tot_rides
	eststo est4
	estimates use ${estpath}stacked_PA_log_firms
	eststo est5
	estimates use ${estpath}stacked_PA_tot_firms
	eststo est6

	label var tot_pmt "\shortstack{Total Ride \\ Payments}"
	label var log_tot_pmt "\shortstack{Total Ride \\ Payments (Log)}"
	label var ambulance_id "\shortstack{Total \\ Rides}"
	label var log_rides "\shortstack{Total \\ Rides (Log)}"
	label var active_firms "\shortstack{Active \\ Firms}"
	label var log_active_firms "\shortstack{Active \\ Firms (Log)}"
	label define post 100 "Prior Auth.", replace
	label values post_together post

	esttab using ${outpath}Table_A4.tex, ///
		replace label se frag booktabs keep(*100*) ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
		stats(ymean N, label("Dep. Var. Mean" "Observations"))

	*Table A5
	estimates clear
	estimates use ${estpath}stacked_civil_log_pmt
	eststo est1
	estimates use ${estpath}stacked_civil_log_rides
	eststo est2
	estimates use ${estpath}stacked_criminal_log_pmt
	eststo est3
	estimates use ${estpath}stacked_criminal_log_rides
	eststo est4

	label define post 100 "Enforcement", replace
	label values post_together post

	esttab using ${outpath}Table_A5.tex, ///
		replace label se frag booktabs keep(*100*) ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
		mgroups("Civil" "Criminal", pattern(1 0 1 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span ///
			erepeat(\cmidrule(lr){@span})) ///
		stats(ymean N, label("Dep. Var. Mean" "Observations"))
}
	
log close